﻿using Puerts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TTEngine;
using TTEngine.Framework.UI;
using TTEngine.Graphics;
using TTEngine.Resource;
using TTEngine.TTMath;

namespace voidbox.Code
{
    public static class Root
    {
        public const string Version = "voidbox v0.1";

        public static Stack<VBApp> AppStack = new Stack<VBApp>();


        public static UIFramework UIFramework;
        public static Batcher Batcher
        {
            get
            {
                return UIFramework.Canvas.Batcher;
            }
        }
        public static UITarget MainArea;

        static UITargetFixer screenfixer;
        public static void Init()
        {
            CopyDep.Init();
            //初始化默认资源和UI资源
            //TTEngine.Runtime.InitDefaultResource();

            var commonresource = new CommonResource();
            commonresource.Init(ResourcePackage.Create());

            UIFramework = new UIFramework(commonresource);
            UIFramework.Init();
            screenfixer = new UITargetFixer();
            screenfixer.SetAsp(320.0f / 240.0f, 320.0f / 240.0f);
            UIFramework.Canvas.AddChild(screenfixer);

            //前景换一个完美缩放Shader
            //DefaultPackage 中的资源无法随时卸载

            screenfixer.matDrawForeGround = new Material(UIFramework.Resource.Package,
                UIFramework.Resource.Package.GetNamedResource<ShaderProgram>("smoothpixel"));
            screenfixer.matDrawForeGround._uniformFloats["borderuv"].Value = 0.1f;//0.1过渡边不明显，不会产生浆糊感觉

            //背景弄个高斯模糊
            MainArea = new UITarget(null, UIFramework.Resource);
            MainArea.SetSize(320, 240);
            MainArea.clearColor = new TTEngine.TTMath.Color(1.0f, 0.3f, 0.8f, 1.0f);

            MainArea.LocalRect.SetAsFill();
            //MainArea.LocalRect.RadioX1 = 0.25f;
            //MainArea.LocalRect.RadioX2 = 0.75f;
            screenfixer.UITarget = MainArea;


            var customRender = new UICustomRender();
            MainArea.Canvas.AddChild(customRender);
            customRender.OnCustomUpdate = OnUpdate;
            customRender.OnCustomRender = OnRender;
            customRender.OnCustomTouch = OnTouch;
            TTEngine.API.view.input.OnKey = OnKey;
            //根据APP 重新调整   screenfixer.SetAsp &   MainArea.SetSize(320, 240);

            //TTEngine.API.view.OnUpdate = OnUpdate;

            StartConfig.Init();

            RunApp(StartConfig.StartApp);



        }
        public static void SetOutputSize(int width, int height)
        {
            float asp = (float)width / (float)height;
            screenfixer.SetAsp(asp, asp);
            MainArea.SetSize((uint)width, (uint)height);
        }
        public static void RunApp(string apppath)
        {
            if (Root.AppStack.Count > 0)
            {
                Root.AppStack.Peek().ToBack();
            }
            var app = new VBApp(apppath);
            Root.AppStack.Push(app);
            Root.AppStack.Peek().Init();
            Root.AppStack.Peek().ToTop();

        }
        static void OnUpdate(float delta, UICanvas canvas, fAABB dest)
        {

            if (AppStack.Count > 0)
            {

                AppStack.Peek().CallUpdate(delta);

            }
        }
        static void OnRender(UICanvas canvas, fAABB dest)
        {
            if (AppStack.Count > 0)
            {

                AppStack.Peek().CallRender();

            }
        }
        //Touch 会被映射到分辨率
        static bool OnTouch(int touchid, bool press, bool move, float x, float y)
        {
            if (AppStack.Count > 0)
            {
                var app = AppStack.Peek();
                app.CallOnTouch(touchid, press, x, y);
            }
            return true;
        }
        static void OnKey(KeyCode key, bool press)
        {
            if (AppStack.Count > 0)
            {
                var app = AppStack.Peek();
                app.CallOnKey((int)key, press);
            }

        }
    }
}
